//
//  UpdateProfile_VC.m
//  KaChing_App
//
//  Created by Jordan Robert Yust on 3/6/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "UpdateProfile_VC.h"


@implementation UpdateProfile_VC

@synthesize userID_TextField;
@synthesize remoteKey_TextField;
@synthesize profileName_TextField;
@synthesize userDictionary;
@synthesize indexToUpdate;

@synthesize settings_TableView;

/*
// Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad
{
	/* Ensure that each TextField does not delete the text already in place when user selects the TextField */
	profileName_TextField.clearsOnBeginEditing = FALSE;
	userID_TextField.clearsOnBeginEditing = FALSE;
	remoteKey_TextField.clearsOnBeginEditing = FALSE;
	
	//turn autocorrect off; these won't be in the dictionary
	profileName_TextField.autocorrectionType = UITextAutocorrectionTypeNo;
	userID_TextField.autocorrectionType = UITextAutocorrectionTypeNo;
	remoteKey_TextField.autocorrectionType = UITextAutocorrectionTypeNo;
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Dismiss the keyboard when the view outside the text field(s) is touched.
    [profileName_TextField resignFirstResponder];
	[userID_TextField resignFirstResponder];
	[remoteKey_TextField resignFirstResponder];
	
    [super touchesBegan:touches withEvent:event];
}

/*
 ------------------------------------------------------------------------
 UITextFieldDelegate protocol methods
 ------------------------------------------------------------------------
 */

#pragma mark -
#pragma mark UITextFieldDelegate protocol methods

/* This method is called whenever the user taps the return method.
 NOTE: This method is part of the UITextFieldDelegate Protocol.
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	/* Resign First Responder (hide the keyboard) */
	if (textField == self.profileName_TextField || textField == self.remoteKey_TextField || textField == self.userID_TextField)
	{
		[textField resignFirstResponder];
	}
	
	return YES;
}

/*
 ------------------------------------------------------------------------
IBAction methods
 ------------------------------------------------------------------------
 */

#pragma mark -
#pragma mark IBAction methods

/*
 This method updates the profile at the specified dictionary index to the current contents in the TextFields.
 */
-(IBAction) updateProfile
{
	[profileName_TextField resignFirstResponder];
	[userID_TextField resignFirstResponder];
	[remoteKey_TextField resignFirstResponder];
	
	/* Update each characteristic of the profile at the dictionary location with the current contents in the TextFields */
	[[userDictionary objectForKey:@"ProfileNames"] replaceObjectAtIndex:indexToUpdate withObject:profileName_TextField.text];
	[[userDictionary objectForKey:@"UserIDs"] replaceObjectAtIndex:indexToUpdate withObject:userID_TextField.text];
	[[userDictionary objectForKey:@"RemoteKeys"] replaceObjectAtIndex:indexToUpdate withObject:remoteKey_TextField.text];
	
	NSLog(@"Name: %@    ID: %@   Key: %@", profileName_TextField.text, userID_TextField.text, remoteKey_TextField.text);
	/* Save the contents of the updated profile as defaults (to ensure information is saved between application launchings) */
	NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];		
	[preferences setObject:userDictionary forKey:@"UserDictionary"];
	
	/* Update the profiles in the settings view (in order to correctly display the changes to the edited profile) */
	[settings_TableView reloadData];
	
	/* Pop this view of the navigation controller's stack.  This transitions back to the previous view. */
	[self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc
{
	/* To adhere to memory management, release each global field of this class. */
	[userID_TextField release];
	[remoteKey_TextField release];
	[profileName_TextField release];
	[userDictionary release];
	[settings_TableView release];
    [super dealloc];
}

@end
